#include <soc.h>
#include "cli_api.h"
#include "device_cmds.h"

extern void codec_speaker_offon(uint32_t enable);
static void device_command_usage(const char *prog)
{
    printf("  Usage: %s [OPTIONS]\r\n", prog);
    printf("  Options:\r\n");
    printf("  get SN                                    Get device SN number\r\n");
    printf("  set SN                                    Set device SN number\r\n");
    printf("\r\n");
}

#define SN_PREFIX "ST51"
int get_device_SN(uint8_t *sn, uint32_t *len)
{   
    int32_t ret = 0;
    char sn_temp[32] = {0};

    if ((NULL == sn) || (NULL == len)) {
        printf("param invalid\n");
        return -1;
    }
    ret = aos_kv_get(DEV_SN_IN_KV, sn, len);
    if (ret == 0) {
        strcpy(sn_temp, SN_PREFIX);
        strcat(sn_temp, sn + strlen(SN_PREFIX));
        strcpy(sn, sn_temp);
        cli_printf("sn len (%d): %s\r\n", *len, sn);
    } else {
        cli_printf("sn get fail\r\n");
    }

    return ret;
}

int set_device_SN(char *sn, uint32_t len)
{
    int32_t ret = 0;

    if ((NULL == sn) || (len == 0)) {
        printf("param invalid\n");
        return -1;
    }
    ret = aos_kv_set(DEV_SN_IN_KV, sn, len, 1);
    if (ret == 0) {
        cli_printf("new sn len (%d) : %s\r\n", len, sn);
    } else {
        cli_printf("sn set fail\r\n");
    }

    return ret;
}

static int get_device_info(int argc, char **argv)
{
    uint32_t ret = 0;
    uint8_t sn[20] = {0};
    uint32_t sn_len = sizeof(sn);

    if (!strcmp(argv[2], "SN")) {
        ret = get_device_SN(sn, &sn_len);
    } else {
        printf("param invalid!\n");
        ret = -1;
    }

    return ret;
}

static int set_device_info(int argc, char **argv)
{
    uint32_t ret = 0;

    if (!strcmp(argv[2], "SN") && (argc == 4)) {
        ret = set_device_SN(argv[3], strlen(argv[3]));
    } else {
        printf("param invalid!\n");
        ret = -1;
    }

    return ret;
}

static int set_speaker_config(int argc, char **argv)
{
    uint32_t ret = 0;

    if (!strcmp(argv[2], "on")) {
        codec_speaker_offon(1);
    } else if (!strcmp(argv[2], "off")) {
        codec_speaker_offon(0);
    } else {
        printf("param invalid!\n");
        ret = -1;
    }

    return ret;
}

void device_command(char *outbuf, int len, int argc, char **argv)
{
    if (argc == 1) {
        device_command_usage(argv[0]);
        return;
    }
    if (!strcmp(argv[1], "get")) {
        get_device_info(argc, argv);
    } else if (!strcmp(argv[1], "set")) {
        set_device_info(argc, argv); 
    } else if (!strcmp(argv[1], "speaker")) {
        set_speaker_config(argc, argv); 
    } else {
       printf("param invalid!\n");
    }

    return;
}

static const struct cli_command_st device_cli_cmd[] = 
{
    [0] =
    {
        .name            = "device",
        .help            = "device [get|set <info>]" \
                           "\r\n device speaker < off | on >",
        .function        = device_command
    },
};

int device_cmds_cli_register(void) 
{
    int ret = 0;

    ret = cli_register_commands(device_cli_cmd, sizeof(device_cli_cmd)/sizeof(struct cli_command_st));
    if (ret != CLI_OK) {
        printf("device cli register fail\n");
        return ret;
    }

    return CLI_OK;
}